package com.appchina.autism.dailylife;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import com.appchina.autism.engine.SoundPoolEngine;
import com.appchina.autism.engine.VibratorEngine;
import com.appchina.autism.widget.ItemView;

public class MainActivity extends Activity implements OnClickListener{
	
	private View mActionBarLeft;
	private View mActionBarRight;
	
	
	private int [] itemViewIds = {R.id.item_1, R.id.item_2,
			R.id.item_3, R.id.item_4, R.id.item_5, R.id.item_6};
	
	private int [] itemViewImgIds = {R.drawable.temp, R.drawable.temp,
			R.drawable.temp, R.drawable.temp,
			R.drawable.temp, R.drawable.temp,};
	
	
	private ArrayList<ItemView> itemViews;

	ArrayList<Integer> mSoundIdList;
	SoundPoolEngine soundPoolEngine;
	VibratorEngine vibratorEngine;
	
	private int index = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		soundPoolEngine = SoundPoolEngine.getInstance(this);
		vibratorEngine = VibratorEngine.getInstance(this);
		
		mActionBarLeft = findViewById(R.id.common_widget_action_bar_left);
		mActionBarLeft.setOnClickListener(this);
		mActionBarRight = findViewById(R.id.common_widget_action_bar_right);
		mActionBarRight.setOnClickListener(this);
		
		itemViews = new ArrayList<ItemView>();
		for(int i = 0; i < itemViewIds.length; ++i) {
			itemViews.add((ItemView)findViewById(itemViewIds[i]));
		}
		
		String [] itemViewTexts = getResources().getStringArray(R.array.daily_life);
		int i = 0;
		for(ItemView itemView : itemViews) {
			itemView.setContent(itemViewImgIds[i], itemViewTexts[i]);
			itemView.setOnClickListener(this);
			++i;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		int [] soundIds = {R.raw.s1_11};
		mSoundIdList = soundPoolEngine.loadRes(soundIds);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		soundPoolEngine.release();
		super.onPause();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.common_widget_action_bar_left:
//			VibratorEngine.getInstance(MainActivity.this).vibrate();
			break;
		case R.id.common_widget_action_bar_right:
			break;
		case R.id.item_1:
		case R.id.item_2:
		case R.id.item_3:
		case R.id.item_4:
		case R.id.item_5:
		case R.id.item_6:
		    int index = 0;
		    for(int i = 0; i < itemViewIds.length; ++i) {
		    	if(v.getId() == itemViewIds[i]) {
		    		index = i;
		    		break;
		    	}
		    }
			DetailActivity.launch(MainActivity.this, index);
			break;
		default:
			break;
		}
	}

}
